﻿



import Request from '../../base/request.js?t=202511041006'
import Storage from '../../base/storage.js?t=202511041006'

import { dealTime, queryURLParams, IgetDeptList, getBrowserInfo, previewCom, popShowCom, getRecentYears} from "../../base/utils.js?t=202511041006"
const Composition = (proxy) => {


    const data = reactive({
        messageTest: "",
        proxy: "",
        popTitle: "",
        cIndex: 0,
        previewInfo: {
            pdfurl: "",
            pdfOpen: false,
            previewShow: false,
            imgUrl: ""
        },
        userinfo: {
        },
        tabList: [
            {
                label: '事故管理', value: 0
            }],
        queryParams: {
      
        },
        yearList:[],
        urlinfo: {},
        uploadLock: false,
        opType: true,
        popShow: false,
        pagination: {
            total: 0,
            current: 1,
            pageSize: 10,
            loading: true,
            showTotal: total => `总共 ${total} 项`,
            defaultPageSize: 10,
            showSizeChanger: true, // 是否显示pagesize选择
            showQuickJumper: false, // 是否显示跳转窗
        },
        levelList: [

        ],

        citem: {},
        deptList:[],
        columns: [
            {
                title: '序号',
                dataIndex: 'index',
                key: 'index',
                align: 'center',
                width: 80,
                customRender: ({ index }) => {
                    return (params.pagination.value.current - 1) * params.pagination.value.pageSize + index + 1
                },
            },

            {
                title: '年度',
                dataIndex: 'Years',
                key: 'Years',
                align: 'center',
                customRender: function ({ text }) {
                    return text
                }
            },
            {
                title: '事故数量',
                dataIndex: 'Counts',
                key: 'Counts',
                align: 'center',
                customRender: function ({ text }) {

                    return text
                }
            },
            {
                title: '更新时间',
                dataIndex: 'UpdateTime',
                key: 'UpdateTime',
                align: 'center',
                customRender: function ({ text }) {
                    return dealTime(text,'YYYY-MM-DD HH:mm:ss')
                }
            },
            {
                title: '更新人',
                dataIndex: 'CreateUserName',
                key: 'CreateUserName',
                align: 'center',
            },
            {
                title: '备注',
                dataIndex: 'Remark',
                key: 'Remark',
                align: 'center',
 
            },
            {
                title: '操作',
                key: 'action',
                align: 'center',
                customRender: function ({ text, record, index, column }) {

                    const FDBtn = h(antd.Popconfirm, {
                        title: `提示：${record.Years}更新人【${record.CreateUserName}】事故信息，删除后将无法恢复，请您确认。`,
                        onConfirm: () => methods.handleDelete(record)

                    }, { default: () => [delBtn] })

                    let detailBtn = h(antd.Button, {
                        type: "link",
                        onClick: () => {
                            methods.handleDetail(record)
                        }
                    }, { default: () => '详情' })

                    let editBtn = h(antd.Button, {
                        type: "link",
                        onClick: () => {
                            methods.handleEdit(record)
                        }
                    }, { default: () => '编辑' })

                    let uploadBtn = h(antd.Button, {
                        type: "link",
                        onClick: () => {
                            methods.handleEdit(record)
                        }
                    }, { default: () => '上传' })
                    let delBtn = h(antd.Button, {
                        type: "link",
                        danger:true
                    }, { default: () => '删除' })
                    return h(antd.Space, {

                    },
                        { default: () => [params.userinfo.value.DepartmentId == 20 ? editBtn : null, uploadBtn, params.userinfo.value.DepartmentId == 20 ? FDBtn : null,detailBtn] }
                    );

                }
            },
        ],
        dataSource: [],

    })
    let params = toRefs(data)
    const methods = {
        getDeptList: () => {
            return new Promise(async (resolve, rej) => {
                params.deptList.value = await IgetDeptList(Request)
                if (params.deptList.value) {
                    resolve(1)
                }
            })
        },

        toAdd: () => {
            window.location.href = `/AccidentManagement/AMAdd`
        },
        handleOk: () => {
            params.popShow.value = false

        },
        handleDetail: (item) => {
            window.location.href = `/AccidentManagement/AMAdd?id=${item.Id}&type=9`
        },
        handlePreview: (item) => {
            if (item.FileName.indexOf('.pdf') != -1) {
                params.previewInfo.value.pdfUrl = item.FilePath
                params.previewInfo.value.pdfOpen = true
            } else {
                params.previewInfo.value.imgUrl = item.FilePath
                params.previewInfo.value.previewShow = true
            }
        },

        handleEdit: (item) => {
            window.location.href = `/AccidentManagement/AMAdd?id=${item.Id}&type=1`
        },

        handleDelete: (item) => {
            Request({
                url: "/AccidentManagement/DeleteAccidentForm",
                method: 'post',
                data: {
                    Id: item.Id
                }
            }).then(res => {
                params.messageTest.value.success(res.Msg);
                methods.getTableList(true)
            })

        },









        handleTableChange: (page) => {
            const { current, pageSize } = page
            params.pagination.value.current = current;
            params.pagination.value.pageSize = pageSize;
            methods.getTableList()
        },
   
        getTableList: async (isRresh) => {
            if (isRresh) {
                params.pagination.value.current = 1
            }
            params.pagination.value.loading = true
            Request({
                url: "/AccidentManagement/GetAccidentFormPageList",
                method: 'post',
                params: {
                    page: params.pagination.value.current,
                    pageSize: params.pagination.value.pageSize
                },
                data: {
                    ...params.queryParams.value
                }
            }).then(res => {
                params.dataSource.value = res.Data.Data
                params.pagination.value.total = res.Data?.TotalItemCount
            }).finally(() => {
                params.pagination.value.loading = false
            })
        },
        filterOption: (val, type) => {
            return `${type.label}`.includes(`${val || ''}`)
        },


        init: () => {
            getBrowserInfo().catch(err => { console.log(err) })
            if (proxy) {
                params.proxy.value = proxy
            }
      


            const key = `${window.location.pathname}Index`;
            const storedValue = Storage.get(key, true); // 获取原始值（字符串或 null）

            if (storedValue !== null && storedValue !== undefined && !isNaN(storedValue)) {
                params.cIndex.value = +Storage.get(`${window.location.pathname}Index`, true)
            }
            params.urlinfo.value = queryURLParams()
            params.yearList.value = getRecentYears()
            const { message } = antd
            params.messageTest.value = message
            params.messageTest.value.config({
                top: `10vh`,
                duration: 7,
                maxCount: 1,
                rtl: true,
            });
            setTimeout(() => {
                let info = Storage.get('AccountInfo', true)
                params.userinfo.value = info
                Request({
                    url: "/Account/createJwtToken",
                    method: 'post',
                    data: {
                        "userName": info && info.UserNo
                    }
                }).then(async res => {
                    Storage.set('token', res.Data, true)
                    methods.getDeptList()
                    methods.getTableList(true)
 
                })
            }, 400)

        },

    }

    const template = {
        imageModel: previewCom(params, methods).imageModel,
        pdfPopModel: previewCom(params, methods).pdfPopModel,

    }
    return {
        ...params,
        ...methods,
        template
    }
}

export default Composition